package com.sv.jm.baselibrary.base;

import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sv.jm.baselibrary.R;
import com.sv.jm.baselibrary.event.EventAction;
import com.sv.jm.baselibrary.event.EventActionKey;
import com.sv.jm.baselibrary.utils.FastClickCheck;
import com.sv.jm.baselibrary.utils.LogHelper;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * 作者: 胡金辉 on 2017/6/9.
 * 邮箱: 443080226@qq.com
 */

public abstract class BaseFragment extends Fragment implements IBase, View.OnClickListener {
    View vsLoading, vsContainer, vsError;
    ViewStub vs_loading, vs_container, vs_error;
    View rootView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = View.inflate(getContext(), R.layout.fragment_layout, null);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setContainerLayout();
        OnFragmentCreated();
    }

    @Override
    public void onClick(View v) {
        if (FastClickCheck.isFaskClick())//误操作检测
            return;
        otherClick(v);
    }

    @Override
    public void setListeners() {

    }

    @Override
    public void setDatas() {

    }

    /**
     * 加载数据页面初始化
     */
    private void initStubLoding() {
        if (null == vsLoading) {
            vs_loading = findView(rootView, R.id.vs_loading);
            vsLoading = vs_loading.inflate();
        }
    }

    /**
     * 异常页面初始化
     */
    private void initStubError() {
        if (null == vsError) {
            vs_error = findView(rootView, R.id.vs_error);
            vsError = vs_error.inflate();
            LinearLayout errorLy = findView(vsError, R.id.errorLy);
            errorLy.setOnClickListener(this);
        }
    }

    /**
     * 显示加载页面
     *
     * @param msg 显示加载提示信息  为空使用默认设置
     */
    public void showLoading(String msg) {
        initStubLoding();
        TextView txtLoading = findView(vsLoading, R.id.txtLoading);
        if (!TextUtils.isEmpty(msg))
            txtLoading.setText(msg);
        vsLoading.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏加载页面
     */
    public void hideLoading() {
        if (null != vsLoading)
            vsLoading.setVisibility(View.GONE);
    }

    /**
     * 显示异常页面
     *
     * @param msg 显示异常提示信息  为空使用默认设置
     */
    public void showError(String msg) {
        initStubError();
        TextView txtLoading = findView(vsError, R.id.txtError);
        if (!TextUtils.isEmpty(msg))
            txtLoading.setText(msg);
        vsError.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏异常页面
     */
    public void hideError() {
        if (null != vsError)
            vsError.setVisibility(View.GONE);
    }

    /**
     * 设置页面内容布局
     */
    public abstract int getFragmentLayout();

    public void setContainerLayout() {
        if (null == vsContainer) {
            int layoutId = getFragmentLayout();
            if (layoutId > 0) {//正确获取布局文件id
                vs_container = findView(rootView, R.id.vs_container);
                vs_container.setLayoutResource(layoutId);
                vsContainer = vs_container.inflate();
            } else {
                LogHelper.logE(this, "layout_inflate_error  布局文件加载异常---请关注");
            }
        }
        OnCoustomLayoutCreated(vsContainer);
    }

    /**
     * 内容创建完成后，根据view查找相关组件
     *
     * @param v v为设置内容页面更目录view
     */
    public abstract void OnCoustomLayoutCreated(View v);

    /**
     * 初始化方法
     */
    public void OnFragmentCreated() {
        setListeners();
        setDatas();
    }

    //快速查找组件
    public <T> T findView(View v, int id) {
        return (T) v.findViewById(id);
    }


    /**
     * 其他点击
     */
    public abstract void otherClick(View v);

    /**
     * 退出程序事件
     *
     * @param eventAction
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void receiveAction(EventAction eventAction) {
        String action = eventAction.getAction();
        if (action.equals(EventActionKey.REFRESH)) {
            onRefreshData();
        } else {
            onOtherActionReceiver(eventAction);
        }
    }

    /**
     * 页面需要刷新的任务放在这里处理
     * 当发送刷新事件接受到后会自动执行刷新任务
     */
    protected abstract void onRefreshData();

    /**
     * 接受到其他总线通知时调用此方法做逻辑处理
     */
    public abstract void onOtherActionReceiver(EventAction eventAction);
}
